package com.vitabuybuy.tv.ui.mine.order

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.core.os.bundleOf
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.library.common.ext.setOnclickNoRepeat
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityOrderDetailBinding
import com.vitabuybuy.tv.dto.OrderDetailDto
import com.library.common.toast.XToast
import com.library.common.util.DensityUtil
import com.library.common.widget.RecycleViewDivider
import com.tencent.imsdk.TIMConversationType
import com.tencent.qcloud.tim.uikit.modules.chat.base.ChatInfo
import com.vitabuybuy.tv.ui.cart.ConfirmOrderActivity
import com.vitabuybuy.tv.ui.cart.OrderPayActivity
import com.vitabuybuy.tv.ui.mine.order.adapter.OrderDetailAdapter
import com.vitabuybuy.tv.ui.mine.order.after.AfterSaleTypeActivity
import com.vitabuybuy.tv.ui.mine.order.viewmodel.OrderViewModel
import com.vitabuybuy.tv.ui.tencent.chat.ChatActivity
import com.vitabuybuy.tv.utils.EventCode
import kotlinx.android.synthetic.main.activity_order_detail.*
import org.greenrobot.eventbus.EventBus

class OrderDetailActivity : BaseActivity<OrderViewModel, ActivityOrderDetailBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_order_detail

    private var orderId:Int? = -1

    private val orderDetailAdapter by lazy { OrderDetailAdapter() }

    override fun onGetBundle(bundle: Bundle?) {
        orderId = bundle?.getInt("orderId")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.order_detail))
        mBinding.apply {
            mViewModel = viewModel
        }
        recyclerView.apply {
            layoutManager = LinearLayoutManager(mContext)
            addItemDecoration(RecycleViewDivider(mContext, DensityUtil.dip2px(6f)))
            adapter = orderDetailAdapter
        }
    }

    override fun observeData() {
        viewModel.getOrderDetail(orderId)
        viewModel.orderDetail.observe(this, Observer {
            when (it.status) {
                OrderListActivity.WAITING_PAY -> {
                    rlDeliver.visibility = View.GONE
                    btnRemindSend.visibility = View.GONE
                    btnCheckDeliver.visibility = View.GONE
                    btnComment.visibility = View.GONE
                    btnConfirmReceive.visibility = View.GONE
                    btnBuyAgain.visibility = View.GONE
                    btnDeleteOrder.visibility = View.GONE
                    btnCancelOrder.visibility = View.VISIBLE
                    btnPay.visibility = View.VISIBLE
                    ll_bottom.visibility = View.VISIBLE
                }
                OrderListActivity.WAITING_SEND -> {
                    rlDeliver.visibility = View.GONE
                    ll_bottom.visibility = View.VISIBLE
                    btnRemindSend.visibility = View.VISIBLE
                    btnCheckDeliver.visibility = View.GONE
                    btnComment.visibility = View.GONE
                    btnConfirmReceive.visibility = View.GONE
                    btnBuyAgain.visibility = View.GONE
                    btnDeleteOrder.visibility = View.GONE
                    btnCancelOrder.visibility = View.GONE
                    btnPay.visibility = View.GONE
                }
                OrderListActivity.WAITING_RECEIVER -> {
                    ll_bottom.visibility = View.VISIBLE
                    rlDeliver.visibility = View.VISIBLE
                    btnRemindSend.visibility = View.GONE
                    btnCheckDeliver.visibility = View.VISIBLE
                    btnComment.visibility = View.GONE
                    btnConfirmReceive.visibility = View.VISIBLE
                    btnBuyAgain.visibility = View.GONE
                    btnDeleteOrder.visibility = View.GONE
                    btnCancelOrder.visibility = View.GONE
                    btnPay.visibility = View.GONE
                }
                OrderListActivity.WAITING_COMMENT -> {
                    ll_bottom.visibility = View.VISIBLE
                    rlDeliver.visibility = View.VISIBLE
                    btnRemindSend.visibility = View.GONE
                    btnCheckDeliver.visibility = View.VISIBLE
                    btnComment.visibility = View.VISIBLE
                    btnConfirmReceive.visibility = View.GONE
                    btnBuyAgain.visibility = View.VISIBLE
                    btnDeleteOrder.visibility = View.GONE
                    btnCancelOrder.visibility = View.GONE
                    btnPay.visibility = View.GONE
                }
                OrderListActivity.ALREADLY_CANCEL -> {
                    rlDeliver.visibility = View.GONE
                    btnRemindSend.visibility = View.GONE
                    btnCheckDeliver.visibility = View.GONE
                    btnComment.visibility = View.GONE
                    btnConfirmReceive.visibility = View.GONE
                    btnBuyAgain.visibility = View.GONE
                    ll_bottom.visibility = View.VISIBLE
                    btnDeleteOrder.visibility = View.VISIBLE
                    btnCancelOrder.visibility = View.GONE
                    btnPay.visibility = View.GONE
                }
                OrderListActivity.ALREADLY_CLOSE -> {
                    ll_bottom.visibility = View.VISIBLE
                    rlDeliver.visibility = View.VISIBLE
                    btnRemindSend.visibility = View.GONE
                    btnCheckDeliver.visibility = View.VISIBLE
                    btnComment.visibility = View.GONE
                    btnConfirmReceive.visibility = View.GONE
                    btnBuyAgain.visibility = View.VISIBLE
                    btnDeleteOrder.visibility = View.GONE
                    btnCancelOrder.visibility = View.GONE
                    btnPay.visibility = View.GONE
                }
                else -> {
                    ll_bottom.visibility = View.GONE
                    rlDeliver.visibility = View.VISIBLE
                }
            }
            val childList = ArrayList<OrderDetailDto>()
//            if (it.status != OrderListActivity.WAITING_PAY &&
//                it.status != OrderListActivity.ALREADLY_CANCEL
//            ) {
            if(it.childList == null || it.childList.size == 0){
                childList.add(it)
                it.childList = childList
            }
            orderDetailAdapter.setNewData(it.childList)
        })
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0, 2, 3 -> {
                when (msg.arg1) {
                    1 -> XToast.success(getString(R.string.order_already_cancel))
                    2 -> XToast.success(getString(R.string.order_already_receiver))
                }
                if (msg.code != 0) {
                    EventBus.getDefault().post(MessageEvent(EventCode.update_order_list))
                }
                viewModel.getOrderDetail(orderId)
            }
            4 -> {
                XToast.success(getString(R.string.already_remind_send))
            }
            5 -> {
                EventBus.getDefault().post(MessageEvent(EventCode.update_order_list))
                XToast.success(getString(R.string.already_delete_order))
                finish()
            }
        }
    }

    override fun eventHandling() {
        setOnclickNoRepeat(
            btnRemindSend, btnCheckDeliver, btnComment, btnConfirmReceive
            , btnBuyAgain, btnDeleteOrder, btnCancelOrder, btnPay,rlDeliver
        ){v->
            when (v) {
                btnPay -> {
                    OrderPayActivity.openActivity(
                        mContext,
                        orderId,
                        viewModel.orderDetail.value?.goodsAmount,
                        OrderPayActivity.FROM_PAY_ORDER
                    )
                }
                btnCancelOrder -> {
                    AlertDialog.Builder(mContext)
                        .setTitle(getString(R.string.reminder))
                        .setMessage(getString(R.string.are_you_sure_cancel_order))
                        .setCancelable(true)
                        .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                            viewModel.cancelOrder(orderId)
                        }
                        .setNegativeButton(getString(R.string.cancel), null).create().show()
                }
                btnDeleteOrder -> {
                    AlertDialog.Builder(mContext)
                        .setTitle(getString(R.string.reminder))
                        .setMessage(getString(R.string.are_you_sure_delete_order))
                        .setCancelable(true)
                        .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                            viewModel.deleteOrder(orderId, 0)
                        }
                        .setNegativeButton(getString(R.string.cancel), null).create().show()
                }
                btnRemindSend -> {
                    viewModel.remindOrder(orderId)
                }
                btnConfirmReceive -> {
                    AlertDialog.Builder(mContext)
                        .setTitle(getString(R.string.reminder))
                        .setMessage(getString(R.string.are_you_sure_confir_receiver))
                        .setCancelable(true)
                        .setPositiveButton(getString(R.string.confirm)) { _, _ ->
                            viewModel.receiverOrder(orderId)
                        }
                        .setNegativeButton(getString(R.string.cancel), null).create().show()
                }
                btnBuyAgain -> {
                    orderId?.run {
                        ConfirmOrderActivity.openActivity(
                            mContext, arrayListOf(this),
                            ConfirmOrderActivity.BUY_AGAIN
                        )
                    }
                }
                btnComment -> {
                    viewModel.orderDetail.value?.run {
                        OrderCommentActivity.openActivity(
                            mContext, this.itemList,
                            this.store.name
                        )
                    }
                }
                btnCheckDeliver,rlDeliver -> {
                    LogisticsInforActivity.openActivity(mContext,orderId)
                }
            }
        }
        orderDetailAdapter.onClickAfterSale = {parentPos, childPos ->
            AfterSaleTypeActivity.openActivity(mContext,orderDetailAdapter.mData[parentPos].itemList[childPos])
        }
        orderDetailAdapter.onClickCallShopListener ={
            val chatInfo = ChatInfo().apply {
                type = TIMConversationType.C2C
                id = orderDetailAdapter.mData[it].store.id.toString()
                chatName = orderDetailAdapter.mData[it].store.name
            }
            ChatActivity.openActivity(mContext,chatInfo)
        }
    }

    override fun onEvent(event: MessageEvent) {
        when (event.code) {
            EventCode.update_order_detail -> {
                viewModel.getOrderDetail(orderId)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, orderId: Int?) {
            mContext.startActivity(
                bundleOf(Pair("orderId", orderId)),
                OrderDetailActivity::class.java
            )
        }
    }
}
